$(document).ready(function() {
    // 检查系统状态
    $.getJSON('/test/hello', function(data) {
        if (data && data.status === 'OK') {
            $('#systemStatus').html('<div class="alert alert-success">系统正常运行</div>');
        } else {
            $('#systemStatus').html('<div class="alert alert-danger">系统运行异常</div>');
        }
    }).fail(function() {
        $('#systemStatus').html('<div class="alert alert-danger">无法连接到系统</div>');
    });
    
    // 检查配置
    $.getJSON('/test/config', function(data) {
        let html = '<table class="table">';
        html += '<tr><th>配置项</th><th>状态</th></tr>';
        html += '<tr><td>应用名称</td><td>' + data.appName + '</td></tr>';
        html += '<tr><td>Bucket</td><td>' + data.bucket + '</td></tr>';
        html += '<tr><td>Domain</td><td>' + data.domain + '</td></tr>';
        html += '<tr><td>AccessKey</td><td>' + (data.accessKeyValid ? '有效' : '无效') + '</td></tr>';
        html += '<tr><td>SecretKey</td><td>' + (data.secretKeyValid ? '有效' : '无效') + '</td></tr>';
        html += '</table>';
        
        $('#configStatus').html(html);
    }).fail(function() {
        $('#configStatus').html('<div class="alert alert-danger">获取配置失败</div>');
    });
    
    // 上传测试
    $('#uploadForm').submit(function(e) {
        e.preventDefault();
        
        let formData = new FormData();
        let fileInput = $('#file')[0];
        
        if (fileInput.files.length === 0) {
            $('#uploadResult').html('<div class="alert alert-warning">请先选择文件</div>');
            return;
        }
        
        formData.append('file', fileInput.files[0]);
        
        $('#uploadResult').html('<div class="alert alert-info">上传中...</div>');
        
        $.ajax({
            url: '/test/upload',
            type: 'POST',
            data: formData,
            processData: false,
            contentType: false,
            success: function(response) {
                let html = '<div class="alert alert-' + (response.success ? 'success' : 'danger') + '">';
                html += response.message;
                html += '</div>';
                
                if (response.success) {
                    html += '<div class="mt-2"><strong>文件URL:</strong> <a href="' + response.url + '" target="_blank">' + response.url + '</a></div>';
                }
                
                $('#uploadResult').html(html);
            },
            error: function() {
                $('#uploadResult').html('<div class="alert alert-danger">上传请求失败</div>');
            }
        });
    });
}); 